package com.mtt.erp.job.task;

import com.google.common.collect.Lists;
import com.mtt.erp.goods.dto.MaaFileTaskDto;
import com.mtt.erp.goods.dto.MabFileTaskDto;
import com.mtt.erp.goods.service.impl.MaaFileServiceImpl;
import com.mtt.erp.goods.service.impl.MabFileServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * User: Z_Shuan
 * Date: 2019/7/29
 * Description:定时任务-->未关联到主商品的子商品执行删除操作
 */
@Slf4j
@Component
public class MabFileTask {

    @Autowired
    private MabFileServiceImpl mabFileService;
    @Autowired
    private MaaFileServiceImpl maaFileService;

    public void deleteMbaFileTask(){
        //定义要删除的子商品id的list集合
        List<String> ids = Lists.newArrayList();
        // 1.查出主商品和子商品的集合
        List<MaaFileTaskDto> maaList = this.maaFileService.getMaaList();
        List<MabFileTaskDto> mabList = this.mabFileService.getMabList();
        List<String> skus = Lists.newArrayList();
        // 2.把sku从maa对象的集合中取出
        for (MaaFileTaskDto maaFileTaskDto : maaList) {
            skus.add(maaFileTaskDto.getMaa01());
        }
        // 3.判断子商品和主商品sku,如果skus包含子商品的sku说明此子商品有与之对应的主商品反之则记录此子商品的id(mab00)
        for (int i = 0; i < mabList.size(); i++) {
            boolean isContains = skus.contains(mabList.get(i).getMab02());
            if(!isContains){
                ids.add(mabList.get(i).getMab00());
            }
        }
        // 4.判断结束,根据ids删除不存在主商品的子商品
        String[] mab00s = new String[ids.size()];
        ids.toArray(mab00s);
        this.mabFileService.deleteMabFile(mab00s);
    }
}
